package com.partsoft.webs.spring.zks;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.zkoss.zk.ui.Executions;

public class VariableResolver implements org.zkoss.xel.VariableResolver {

	public Object resolveVariable(String name) {
		if ("bsh".equals(name))
			return null;
		try {
			ServletContext sct = (ServletContext) Executions.getCurrent().getDesktop().getWebApp().getServletContext();
			WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(sct);
			if ("spring".equals(name)) {
				return wac;
			}
			return wac.getBean(name);
		} catch (NoSuchBeanDefinitionException e) {
			return null;
		}
	}

}
